package com.sc.nft.config.fulu.model;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sc.nft.config.fulu.FuLuConfig;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liGen
 * @version 1.0
 * @date 2023/11/7 3:07 下午
 */
@Data
public class FuLuBaseRequest {

    /**
     * 商户的app_key
     */
    String app_key;

    /**
     * 接口方法名称
     */
    String method;

    /**
     * 时间戳，格式为：yyyy-MM-dd HH:mm:ss
     */
    String timestamp;

    /**
     * 调用的接口版本
     */
    String version;

    /**
     * 接口请求或响应格式
     */
    String format;

    /**
     * 请求使用的编码格式，如utf-8等
     */
    String charset;

    /**
     * 签名加密类型，目前仅支持md5
     */
    String sign_type;

    /**
     * 签名串
     */
    String sign;

    /**
     * 授权码，固定值为“”
     */
    String app_auth_token;

    /**
     * 请求参数集合（注意：该参数是以json字符串的形式传输）
     */
    String biz_content;

    public static FuLuBaseRequest build(String bizContent,String method){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = simpleDateFormat.format(new Date());
        FuLuBaseRequest fuLuBaseRequest = new FuLuBaseRequest();
        fuLuBaseRequest.setApp_key(FuLuConfig.appKey);
        fuLuBaseRequest.setMethod(method);
        fuLuBaseRequest.setTimestamp(timestamp);
        fuLuBaseRequest.setVersion(FuLuConfig.version);
        fuLuBaseRequest.setFormat(FuLuConfig.format);
        fuLuBaseRequest.setCharset(FuLuConfig.charset);
        fuLuBaseRequest.setSign_type(FuLuConfig.signType);
        fuLuBaseRequest.setApp_auth_token(FuLuConfig.appAuthToken);
        fuLuBaseRequest.setBiz_content(bizContent);
        String sign = sign(method, timestamp, bizContent);
        fuLuBaseRequest.setSign(sign);
        return fuLuBaseRequest;
    }


    /**
     * 生成签名
     *
     * @param method
     * @param timestamp
     * @param bizContent
     * @return
     */
    public static String sign(String method, String timestamp, String bizContent) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("app_key", FuLuConfig.appKey);
        map.put("method", method);
        map.put("timestamp", timestamp);
        map.put("version", FuLuConfig.version);
        map.put("format", FuLuConfig.format);
        map.put("charset", FuLuConfig.charset);
        map.put("sign_type", FuLuConfig.signType);
        map.put("app_auth_token", FuLuConfig.appAuthToken);
        map.put("biz_content", bizContent);
        JSONObject resultJson = JSON.parseObject(JSON.toJSONString(map));
        char[] s = JSONObject.toJSONString(resultJson, SerializerFeature.WriteMapNullValue).toCharArray();
        Arrays.sort(s);
        String outputSignOriginalStr = new String(s) + FuLuConfig.appSecret;
        String sign = SecureUtil.md5(outputSignOriginalStr);
        return sign;
    }

    public static void main(String[] args) {
       String s =  "{\"order_id\":\"19123033155353051423\",\"product_id\":10000585,\"product_name\":\"视频/文娱直充测试商品-返回充值失败\",\"charge_account\":\"888888\",\"customer_order_no\":\"20191230142306472\",\"create_time\":\"2019-12-30 14:23:04\",\"buy_num\":1,\"order_price\":1.0300,\"order_state\":\"untreated\",\"finish_time\":null,\"area\":\"\",\"server\":\"\",\"type\":\"\",\"order_type\":4,\"operator_serial_number\":\"\"}";
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        String outputSignOriginalStr = new String(chars) + "0a091b3aa4324435aab703142518a8f7";
        String sign = SecureUtil.md5(outputSignOriginalStr);
        System.out.println(sign);
    }
}
